<template>
  <div id="app">
    <div class="app-container">
      <div class="content1">
        <div class="card" style="float: left">
          <div class="img"><img src="../../assets/imgs/qr.png" style="width: 83px; height: 83px; border-radius: 50%;"></div>
          <h1>二维码生成</h1>
          <p><el-button type="success" size="mini" @click="openDialog()">GO</el-button></p>
        </div>

        <el-dialog
          :visible.sync="dialogFormVisible"
          title="请输入二维码内容">
          <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 100%; margin-left: -50px;">
            <el-form-item prop="textarea">
              <el-input
                v-model="temp.textarea"
                type="textarea"
                placeholder="这里输入的内容转换成二维码"
                maxlength="300"
                rows="6"
                class="qr-input"
              >
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="dialogFormVisible=false">取消</el-button>
              <el-button type="primary" @click="createQRCode()">确认</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {getQRCodeImage} from '@/api/qr'
import { uuid } from 'vue-uuid'

export default {
  name: 'QRCode',
  data() {
    return {
      dialogFormVisible: false,
      temp: {
        textarea: ''
      },
      rules: {
        textarea: [
          {required: true, message: '请输入二维码内容', trigger: 'blur'},
          {min: 1, max: 300, message: '长度在1~300个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    openDialog () {
      this.dialogFormVisible = true
    },
    createQRCode() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          this.dialogFormVisible = false
          getQRCodeImage(tempData).then(response => {
            // 获取文件名
            const blob = new Blob([response.data])
            // 创建一个a标签并设置href属性，之后模拟人为点击下载文件
            const link = document.createElement('a')
            link.href = window.URL.createObjectURL(blob)
            link.download = uuid.v1() + '.png'// 生成uuid,设置下载文件名
            link.click()// 模拟点击
            // 释放资源并删除创建的a标签
            URL.revokeObjectURL(link.href)
            document.body.removeChild(link)
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.card {
  width: 190px;
  height: 240px;
  background: #7f27c7;
  border-radius: 16px;
  text-align: center;
  color: #f0f0f0;
  font-family: inherit;
  z-index: 1;
  margin-right: 16px;
}
.img {
  display: flex;
  margin: 60px auto 10px auto;
  width: 85px;
  height: 85px;
  border: 1px solid #f0f0f0;
  border-radius: 50%;
  font-size: 11px;
  justify-content: center;
  align-items: center;
  transition: all 0.5s;
  z-index: 99;
}
p{
  opacity: 0;
  transition: all 0.5s;
}
h1{
  padding: 10px;
  font-weight: 500;
  font-size: 18px;
  color: white;
}
.card:hover .img {
  transform: scale(1.1);
  border: 1px solid #f0f0f0;
}
.card:hover h1,
.card:hover p {
  opacity: 1;
}
/*.qr-input {*/
/*  width: 360%;*/
/*}*/
</style>
